/**
 * 参数设置管理初始化
 */
var CommunityUser = {
    id: "communityUserTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
CommunityUser.initColumn = function () {
    var columns = [
        {field: 'selectItem',visible:false},
        {title: 'id', field: 'id', align: 'center', valign: 'middle',width:'50px'},
        {title: '用户账号', field: 'account', align: 'center', valign: 'middle'},
        {title: '邀请上限数', field: 'invite_user_max_count', align: 'center', valign: 'middle',formatter: function (value, row, index) {
            return '<input class="form-control"  name="invite_user_max_count" type="text" value="'+value+'">';
        }},
        {title: '社区编号', field: 'community_code', align: 'center', valign: 'middle',formatter: function (value, row, index) {
            return '<input class="form-control"  name="community_code" type="text" value="'+value+'">';
        }},
        {title: '创建时间', field: 'add_time', align: 'center', valign: 'middle',sortable : true},
        {title: '状态', field: 'status', align: 'center', valign: 'middle',formatter: function (value, row, index) {
        	 if(value=='0')
        	 {
        			return '<input name="my-checkbox" type="checkbox" checked/>';
        	 }else
        	 {
        			return '<input name="my-checkbox" type="checkbox"/>';
        	 }
        
        }}
    ]
    return columns;
};

/**
 * 检查是否选中
 */
CommunityUser.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中需要修改的记录！");
        return false;
    }else{
        return true;

    }
};

/**
 * 添加
 */
CommunityUser.add = function () {
   if($("#phone").val()=="")
   {
	   return;
   }
	Feng.confirm("您确认要添加该用户为社区管理员吗?",function()
			{
	    //提交信息
	    var ajax = new $ax(Feng.ctxPath + "/communityUser/add", function (data) {
	        if(data.code == 200) {
	            Feng.success("添加成功!");
	            CommunityUser.table.refresh();
	        }else{
	            Feng.error(data.message);
	        }
	       
	    }, function (data) {
	        Feng.error("添加失败!" + data.responseJSON.message + "!");
	    });
	    ajax.setData({"phone":$("#phone").val()});
	    ajax.start();
			});

};

CommunityUser.search = function () {
    var queryData = {};
    queryData['phone'] = $("#search_phone").val();
    CommunityUser.table.refresh({query: queryData});
};

/**
 * 修改
 */
CommunityUser.update = function () {
			Feng.confirm("您确认修改所有的记录吗?",function()
			{
				var is_result=false;
				 var ids = new Array();  
				 var invite_user_max_counts = new Array(); 
				 var community_codes = new Array(); 
				 $("input[name='invite_user_max_count']").each(function()
								{
							   ids.push($(this).parent().parent().find("td").eq(0).text()); 
							   var re = /^[1-9]+[0-9]*]*$/;
							   if (!re.test($(this).val()))
							   {
								   is_result=true;
								   return false;
							   }
							   invite_user_max_counts.push($(this).val());  
							   community_codes.push($(this).parent().parent().find("td").eq(3).find("input").eq(0).val()); 
					});
				 if(is_result)
				 {
					 Feng.error("修改的值必须是正整数");
					 return;
				 }
				
			    //提交信息
			    var ajax = new $ax(Feng.ctxPath + "/communityUser/updateBatch", function (data) {
			        if(data.code == 200) {
			            Feng.success("修改成功!");
			        }else{
			            Feng.error(data.message);
			        }
			       
			    }, function (data) {
			        Feng.error("修改失败!" + data.responseJSON.message + "!");
			    });
			    ajax.setData({"ids":ids,"invite_user_max_counts":invite_user_max_counts,"community_codes":community_codes});
			    ajax.start();
			    
			});
};

CommunityUser.exportExcel=function()
{
	 var url = "/communityUser/exportExcel";
	 window.location.href=url;
}


CommunityUser.excel = function () {
    var index = layer.open({
        type: 2,
        title: '批量导入',
        area: ['750px', '650px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/communityUser/excelList'
    });
    this.layerIndex = index;
};


$(function () {
    var defaultColunms = CommunityUser.initColumn();
    var table = new BSTable(CommunityUser.id, "/communityUser/list", defaultColunms);
    table.setPaginationType("server");
    table.setLoadSuccess(function()
    		{
    	 $("[name='my-checkbox']").bootstrapSwitch({ 
       		      onText:"开启",  
       		      offText:"关闭",  
       		      onColor:"success",  
       		      offColor:"danger",  
       		      size:"small",
       		      onSwitchChange : function(event, state) {  
       			   var real_status=0;
       			   if(state==true)
       			   {
       				real_status=0;
       			   }else
       			   {
       				real_status=1;
       			   }
       			    //提交信息
       			    var ajax = new $ax(Feng.ctxPath + "/communityUser/switchStatus", function (data) {
       			        if(data.code == 200) {
       			            Feng.success("修改成功!");
       			        }else{
       			            Feng.error(data.message);
       			        }
       			       
       			    }, function (data) {
       			        Feng.error("修改失败!" + data.responseJSON.message + "!");
       			    });
       			    ajax.setData({"status":real_status,"id":$(this).parent().parent().parent().parent().find("td").eq(0).text()});
       			    ajax.start();
       		   }
            });
    		});
    CommunityUser.table = table.init();
});



